(function($) {
	var groupCode = '',groupDesc = '',locId = '',locDesc = '';
	var type = '';//L请假,E心愿班,S换班,B个人著作
	var sourceDict;
	$.init({
		swipeBack: true //启用右滑关闭功能
	});
	
	$.plusReady(function() {
		$('.mui-scroll-wrapper').scroll({
			indicators: false //是否显示滚动条
		});
		var currentLoc = getLocalData('currentLoc');
		if(currentLoc) {
			groupCode = currentLoc['GroupCode'];
			groupDesc = currentLoc['GroupDesc'];
			locId = currentLoc['LocId'];
			locDesc = currentLoc['LocDesc'];
		} else {
			mui.toast('用户未登录');
			mui.openWindow('logon-shell.html', 'logon-shell');
			return;
		}
		var self = plus.webview.currentWebview();
		sourceDict = self.sourceData;
		// console.log(self.type,JSON.stringify(sourceDict));
		type = self.type;
		if (sourceDict == null) {//清空界面
			clearUI();
			return;
		}
		if (type == 'S') {
			type = 'S1';
		}
		switch (type){//S换班,R我收到的,L请假,E心愿,O加班,H人力资源
			case 'S1':
			case 'S2':
				updateChangeSchUI();
				break;
			case 'E':
				updateWantSchUI();
				break;
			case 'L':
				updateAbsentUI();
				break;
			case 'O':
				updateOverTimeUI();
				break;
			case 'H':
				updateHRUI();
				break;
			case 'B':
				updateBookUI();
				break;
			default:
				clearUI();
				break;
		}
	});

	//换班申请详情
	function updateChangeSchUI(){
		var titleStr = '换班申请';
		var applyer = sourceDict['PerName']; 
		var shifter = sourceDict['ShiftPerName'];
		fillElementText('applyTitle',titleStr);
		fillElementText('startDateT','申请对象：');
		fillElementText('startDate',applyer);
		fillElementText('endDateT','日期班种：');
		fillElementText('endDate',sourceDict['ApplicationDate']+'（' +sourceDict['ApplicationPostDesc'] +'）');
		fillElementText('applyType','换班对象：');
		fillElementText('applySch',shifter);
		fillElementText('applyLenT','日期班种：');
		fillElementText('applyLen',sourceDict['ShiftDate']+'（' +sourceDict['ShiftPostDesc'] +'）');
		updateHeadState();
		//流程
		var shifterName = sourceDict['ShiftPerName'];
		var pid = getLocalData('pinfo').rw;
		var shifterId = sourceDict['ShiftPerId'];
		var ulEl = document.getElementById('ulEl');
		ulEl.innerHTML = '';
		var html = '';
		var flag = sourceDict['Flag'];
		if (flag == 'A' || flag == 'R') {
			var dateTime = sourceDict['ApprovalDate']+'&nbsp;'+sourceDict['ApprovalTime'];
			var perName = sourceDict['ApprovalPerName'];
			var flagDesc = sourceDict['FlagDesc'];
			var reasonStr = sourceDict['ApprovalExplain'];
			var shiftTime = sourceDict['ShiftPerDate']+'&nbsp;'+sourceDict['ShiftPerTime'];
			var shiftReason = sourceDict['ShiftPerExplain'];
			if (flag == 'A') {
				html = getNormalHtmlWithFlag(dateTime,perName,flagDesc,reasonStr,'agree','√');
				html += getNormalHtmlWithFlag(shiftTime,shifterName,'已同意',shiftReason,'agree','√');
			}else if (flag == 'R' && sourceDict['ShiftPerDate'] != '') {//护士长驳回
				html=getNormalHtmlWithFlag(dateTime,perName,flagDesc,reasonStr,'refused','x');
				html +=getNormalHtmlWithFlag(shiftTime,shifterName,'已同意',shiftReason,'agree','√');
			}else if (flag == 'R' && sourceDict['ShiftPerDate'] == '') {//被换班人驳回
				html=getNormalHtmlWithFlag('','护士长','无需处理','','undeal','=');
			  html+=getNormalHtmlWithFlag(dateTime,perName,flagDesc,reasonStr,'refused','x');
			}
		}else if (flag == 'V') {
			html = getNormalHtmlWithFlag('','护士长','未审核','','undeal','=');
			if (type == 'S2' && shifterId == pid) {
				html += getNurHeadVHtml(shifterName,'审核中','');
			}else{
				html += getNormalHtmlWithFlag('',shifterName,'审核中','','processing','=');
			}
		}else if (flag == 'I') {
			if (type != 'S2' && groupCode == 'nurhead') {
				html = getNurHeadVHtml('护士长','审核中','');
			}else{
				html = getNormalHtmlWithFlag('','护士长','审核中','','processing','=');
			}
			var shiftTime = sourceDict['ShiftPerDate']+'&nbsp;'+sourceDict['ShiftPerTime'];
			var shiftReason = sourceDict['ShiftPerExplain'];
			var appName = sourceDict['ShiftPerAPPName'];
			
			html += getNormalHtmlWithFlag(shiftTime,appName,'已同意',shiftReason,'agree','√');
		}
		//发起申请
		var addDateTime = sourceDict['AddDate']+'&nbsp;'+sourceDict['AddTime'];
		var perName = sourceDict['PerName'];
		var reasonStr = sourceDict['LeaveExplain'];
		html += getApplayerHtml(addDateTime,perName,reasonStr);
		ulEl.innerHTML = html;
	}
	//心愿班详情
	function updateWantSchUI(){
		var titleStr = '心愿班';
		var applyTypeStr = '心愿班种：';
		fillElementText('applyTitle',titleStr);
		fillElementText('startDateT','排班日期：');
		fillElementText('startDate',sourceDict['ExpectDate']);
		hiddenElement('endDateT');hiddenElement('endDate');
		fillElementText('applyType',applyTypeStr);
		fillElementText('applySch',sourceDict['ExpectPostDesc']);
		hiddenElement('applyLenT');hiddenElement('applyLen');
		updateHeadState();
		//流程
		updateTableView();
	}
	//请假申请详情
	function updateAbsentUI(){
		var titleStr = '请假申请';
		var applyTypeStr = '请假类型：';
		fillElementText('applyTitle',titleStr);
		fillElementText('applyState',sourceDict['FlagDesc']);
		fillElementText('startDate',sourceDict['StDate']);
		fillElementText('endDate',sourceDict['EndDate']);
		fillElementText('applyType',applyTypeStr);
		fillElementText('applySch',sourceDict['LeaveType']);
		fillElementText('applyLenT','请假时长：');
		fillElementText('applyLen',sourceDict['Days'],'天');
		fillElementText('applyReason',sourceDict['LeaveExplain']);
		updateHeadState();
		//流程
		updateTableView();
	}
	//加班申请详情
	function updateOverTimeUI(){
		var titleStr = '加班申请';
		var applyTypeStr = '加班时长：';
		fillElementText('applyTitle',titleStr);
		fillElementText('startDateT','加班日期：');
		fillElementText('startDate',sourceDict['ExpectDate']);
		fillElementText('endDateT','加班时段：');
		fillElementText('endDate',sourceDict['StTime']+' － '+sourceDict['EndTime']);
		fillElementText('applyType',applyTypeStr);
		fillElementText('applySch',sourceDict['ExpectPostDesc'],'小时');
		hiddenElement('applyLenT');hiddenElement('applyLen');
		updateHeadState();
		//流程
		updateTableView();
	}
	function updateHRUI(){
		updateHeadState();
		fillElementText('applyTitle','个人资料修改');
		var headDetail = document.getElementById('headDetail');
		var spans = headDetail.querySelectorAll('span');
		var arr = [];
		arr = dealHrKeyValue('PersonheadshipDR','职务',arr);
		arr = dealHrKeyValue('PersonNurHeadDate','任职时间',arr);
		arr = dealHrKeyValue('PersonHeight','身高',arr);
		arr = dealHrKeyValue('PersonClothesNO','衣号',arr);
		arr = dealHrKeyValue('PersonShoeNO','鞋号',arr);
		arr = dealHrKeyValue('Personmarriage','婚姻',arr);
		arr = dealHrKeyValue('PersonSchoolAgeDr','第一学历',arr);
		arr = dealHrKeyValue('PersonDegreeDR','学位',arr);
		arr = dealHrKeyValue('PersonPolitydr','政治面貌',arr);
		arr = dealHrKeyValue('PersonTelHand','联系方式',arr);
		arr = dealHrKeyValue('PersonRegNo','护士注册证书号',arr);
		arr = dealHrKeyValue('PersonProfPostDR','专业技术职称',arr);
		arr = dealHrKeyValue('PersonTechPostDate','取得职称时间',arr);
		arr = dealHrKeyValue('PersonPresentZipCode','现住址邮编',arr);
		arr = dealHrKeyValue('PersonAddress','现住地址',arr);
		headDetail.innerHTML = '';
		var spanA = document.createElement('span');
		spanA.setAttribute('class','title');
		spanA.innerText = '个人资料修改';
		var br = document.createElement('br');
		headDetail.appendChild(spanA);
		headDetail.appendChild(br);
		for (var i = 0; i < arr.length;i++) {
			var oneDict = arr[i];
			var keyStr = oneDict['key'];
			if (keyStr.length == 2 && arr.length != 1) {
				keyStr = keyStr.substring (0,1)+'<span>占位</span>' + keyStr.substring(1,2);
			}
				//key
				var spanT = document.createElement('span');
				spanT.setAttribute('class','label');
				spanT.innerHTML = keyStr+'：';
				//新值span
				var spanS = document.createElement('span');
				spanS.setAttribute('class','oldValue');
				if(oneDict['oldValue'] == ''){
					spanS.innerText = '_____';
				}else{
					spanS.innerText = oneDict['oldValue'];
				}
				var spanU = document.createElement('span');
				spanU.setAttribute('class','tempLabel');
				spanU.innerText = '修改为';
				//旧值span
				var spanV = document.createElement('span');
				spanV.setAttribute('class','newValue');
				spanV.innerText = oneDict['value'];
				//换行
				var br = document.createElement('br');
				headDetail.appendChild(br);
				headDetail.appendChild(spanT);
				headDetail.appendChild(spanS);
				headDetail.appendChild(spanU);
				headDetail.appendChild(spanV);
		}
		//流程
		updateTableView();
	}
	
	function dealHrKeyValue(keyStr,descStr,arr){
		var value = sourceDict[keyStr][1];
		var oldValue = sourceDict[keyStr][0];
		if (value != '') {
			var dict = {'key':descStr,'value':value,'oldValue':oldValue};
			arr.push(dict);
		}
		return arr;
	}
	function updateBookUI(){
		updateHeadState();
		fillElementText('applyTitle','个人著作上报');
		fillElementText('startDateT','书名：');
		fillElementText('startDate',sourceDict['PerBookTitle']);
		fillElementText('endDateT','书号：');
		fillElementText('endDate',sourceDict['PerBookNumber']);
		fillElementText('applyType','编审类型：');
		fillElementText('applySch',sourceDict['PerCpyEtTyp']);
		fillElementText('applyLenT','出版日期：');
		fillElementText('applyLen',sourceDict['PerPubDate']);
		fillElementText('reasonT','出版社：');
		fillElementText('applyReason',sourceDict['PerPubAgency']);
		//reasonT
		//流程
		updateTableView();
	}
	
	function updateHeadState(){
		fillElementText('applyState',sourceDict['FlagDesc']);
		fillElementText('applyReason',sourceDict['LeaveExplain']);
		var applyState = document.getElementById('applyState');
		var flag = sourceDict['Flag'];
		var classStr = 'label agreeTip';
		var pid = getLocalData('pinfo').rw;
		var shifterId = sourceDict['ShiftPerId'];
		if (type == 'S1' && flag == 'I' && groupCode == 'nurhead') {
			//护士长审批自己的换班申请
			setupCanOpration();
		}else if (type == 'S2' && flag == 'V' && pid == shifterId) {
			//换班对象审批换班申请
			setupCanOpration();
		}else if(type != 'S1' && type != 'S2'){
			 if (flag == 'V' && groupCode == 'nurhead'){
				//其它类型的护士长审批
				setupCanOpration();
			}else if (flag == 'V' && groupCode == 'hlb' && type == 'B') {
				setupCanOpration();//个人著作
			}
		}
		
		if ( flag == 'V' || flag == 'I' ) {
			classStr = 'label readyTip';
		}else if (flag == 'R') {
			classStr = 'label refusedTip'
		}
		applyState.setAttribute('class',classStr);
	}
	function updateTableView(){
		var ulEl = document.getElementById('ulEl');
		ulEl.innerHTML = '';
		var html = '';
		var flag = sourceDict['Flag'];
		if (flag == 'A' || flag == 'R') {
			var dateTime = sourceDict['ApprovalDate']+'&nbsp;'+sourceDict['ApprovalTime'];
			var perName = sourceDict['ApprovalPerName'];
			var flagDesc = sourceDict['FlagDesc'];
			var reasonStr = sourceDict['ApprovalExplain'];
			var classStr = 'agree';
			var tip = '√';
			if (flag == 'R') {
				classStr = 'refused';
				tip = 'x';
			}
			html = getNormalHtmlWithFlag(dateTime,perName,flagDesc,reasonStr,classStr,tip);
		}else if (flag == 'V') {
			html = getNurHeadVHtml('护士长','审核中','');
			if (type == 'B') {
				html = getNurHeadVHtml('护理部','审核中','');
			}
		}
		//发起申请
		var addDateTime = sourceDict['AddDate']+'&nbsp;'+sourceDict['AddTime'];
		var perName = sourceDict['PerName'];
		var reasonStr = sourceDict['LeaveExplain'];
		if (type == 'H') {
			perName = sourceDict['PersonName'];
			reasonStr = '';
		}else if (type == 'B') {
			reasonStr = '';
		}
		html += getApplayerHtml(addDateTime,perName,reasonStr);
		ulEl.innerHTML = html;
	}
	function getNormalHtmlWithFlag(dateTime,appPerName,flagDesc,reasonStr,classStr,tip){
		var html = '<li class="'+classStr+'"><i class="colorIcon mui-badge">'+tip+'</i>'+
			'<span class="time">'+dateTime+'</span>'+
			'<span class="nurseName">'+appPerName+'</span>'+
			'<span class="status">'+flagDesc+'</span>'+
			'<span class="reason">'+reasonStr+'</span></li>';
		return html;
	}
	
	function getNurHeadVHtml(vType,vStr,vClass){
		var codeStr = groupCode;
		var flag = sourceDict['Flag']
		var pid = getLocalData('pinfo').rw;
		var shifterId = sourceDict['ShiftPerId'];
		if (type == 'S2' && flag == 'V' && pid == shifterId) {
			codeStr = 'nurhead';
		}else if (type == 'B'&&codeStr == 'hlb') {
			codeStr = 'nurhead';
		}
		var html = '<li class="processing">'+
			'<i class="colorIcon mui-badge">=</i>'+
			'<span class="time"></span>'+
			'<span class="nurseName">'+vType+'</span>'+
			'<span class="status">'+vStr+'</span>'+
			'<div class="reason"><textarea id="reasonText" class="reasonText '+
				codeStr+'" placeholder="请输入审批备注"></textarea></div>'+
			'</li>';
		return html;
	}
	function getApplayerHtml(addDateTime,perName,reasonStr){
		var html = '<li class="first">'+
			'<i class="colorIcon mui-badge">√</i>'+
			'<span class="time">'+addDateTime+'</span>'+
			'<span class="nurseName">'+perName+'</span>'+
			'<span class="status">发起申请</span>'+
			'<span class="reason">'+reasonStr+'</span></li>';
		return html;
	}
	
	function agreeRefuseAction(e){
		var id = e.target.id
		var opStr = 'A';
		var pid = getLocalData('pinfo').rw;
		var shifterId = sourceDict['ShiftPerId'];
		if (type == 'S2' && pid == shifterId) {
			opStr = 'I';
		}
		if (id == 'refuseApply') {
			opStr = 'R';
		}
		var rw = sourceDict['rw']
		var userID = getLocalData('pinfo').rw.toString();
		var reasonText = document.getElementById('reasonText');
		if (reasonText.value == '') {
			if (opStr == 'R' ) {
				mui.toast('请输入拒绝理由');
				return;
			}
		}
		plus.nativeUI.showWaiting("正在提交",{modal:false});
		var xmlStr = '<Request>'+rw+'^'+opStr+'^'+userID+'^'+reasonText.value+'^'+type+'</Request>';
		 // console.log(xmlStr);
		postHttpRequest(xmlStr, 'nurse.stop.nurapproval', function(result) {
			plus.nativeUI.closeWaiting();
			 // console.log('result',result);
			var dataDict = eval('(' + result + ')');
			if (dataDict['success'] == true) {
				plus.storage.setItem('detail','1');
				mui.back();
			}else{
				mui.toast('审批失败,请重试');
			}
		}, function(errorStr) {
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}
	
	function fillElementText(eleId,textStr,extrasText){
		var eleE = document.getElementById(eleId);
		if (textStr == null) {
			textStr = '&nbsp;';
		}
		eleE.innerHTML = textStr;
		if (extrasText != null) {
			eleE.innerHTML = textStr+extrasText;
		}
	}
	function hiddenElement(eleId){
		var headDetail = document.getElementById('headDetail');
		var eleE = document.getElementById(eleId);
		var brE = headDetail.querySelector('#'+eleId+' + br');
		if (eleE) {
			headDetail.removeChild(eleE);
		}
		if (brE) {
			headDetail.removeChild(brE);
		}
	}
	
	function setupCanOpration(){
		var scrollWrapper = document.getElementById('scrollWrapper');
		scrollWrapper.style.marginBottom = '51px';
		var refuseApply = document.getElementById('refuseApply');
		refuseApply.style.display = 'block';
		refuseApply.addEventListener('tap', agreeRefuseAction, false);
		var agreeApply = document.getElementById('agreeApply');
		agreeApply.style.display = 'block';
		agreeApply.addEventListener('tap', agreeRefuseAction, false);
	}
	function setupCannotOpration(){
		var scrollWrapper = document.getElementById('scrollWrapper');
		scrollWrapper.style.marginBottom = '0px';
		var agreeApply = document.getElementById('agreeApply');
		agreeApply.style.display = 'none';
		var refuseApply = document.getElementById('refuseApply');
		refuseApply.style.display = 'none';
	}
	
	function clearUI(){
		var scroll = document.getElementById('apply-process');
		scroll.innerHTML = '';
		setupCannotOpration();
	}
})(mui);
/*
 * 显示逻辑
 * 换班申请
 * 1.用户发起申请 换班对象审核中 管理员未审核
 * 2. a)换班对象同意 管理员审核中 b)换班对象拒绝 管理员没有
 * 3.管理员同意/拒绝
 * 
 * 其他
 * 1.用户发起申请 管理员审核中
 * 2.管理员同意/拒绝
 * 
 * 心愿班
 * {'rw':'7','DepId':'85','DepDesc':'QKYXKPTNKBF-全科医学科(普通内科)病房','PerId':'1020','PerName':'赵霞','ExpectDate':'2020-01-16','ExpectPostId':'543','ExpectPostDesc':'节24h','ExpectType':'值班','LeaveExplain':'111111','Flag':'A','FlagDesc':'通过','AddDate':'2020-01-16','AddTime':'16:15:48','ApprovalPerName':'赵霞','ApprovalDate':'2020-01-17','ApprovalTime':'14:04:26','ApprovalExplain':''}
 * 请假申请 审批
 * {"rw":"2","DepId":"85","DepDesc":"PTNKBF-普通内科病房","PerId":"1020","PerName":"赵霞","LeaveType":"----","StDate":"2019-12-25","EndDate":"2019-12-26","Days":"2","LeaveExplain":"Bbbbb","Flag":"R","FlagDesc":"驳回","AddDate":"2019-12-23","AddTime":"20:01:33","ApprovalPerName":"赵霞ApprovalDate","ApprovalTime":"17:14:04","ApprovalExplain":"这是我的审批意见"}
 * 
 * 同意
 	<li class="agree">
		<i class="colorIcon mui-badge">√</i>
		<span class="time">2019-02-23 18:20:00</span>
		<span class="nurseName">张薇薇</span>
		<span class="status">已同意</span>
		<span class="reason">同意</span>
	</li>
 *
 *拒绝
	<li class="refused">
		<i class="colorIcon mui-badge">x</i>
		<span class="time">2019-02-24 08:12:55</span>
		<span class="nurseName">管理员</span>
		<span class="status">已拒绝</span>
		<span class="reason">25号夜班已安排给其他人，黄灿灿可以考虑请假！</span>
	</li>
 * 
 * 审核中
	<li class="processing">
		<i class="colorIcon mui-badge">=</i>
		<span class="time">2019-02-23 17:00:44</span>
		<span class="nurseName">张薇薇</span>
		<span class="status">审核中</span>
		<span class="reason">正在合理化排班</span>
	</li>
	//填写审批备注
	<div class="reason"><textarea id="reasonText" class="reasonText" placeholder="请输入备注"></textarea></div>
 * 
 * 未审核
	<li class="undeal">
		<i class="colorIcon mui-badge">√</i>
		<span class="time">2019-02-23 13:00:44</span>
		<span class="nurseName">张薇薇</span>
		<span class="status">未审核</span>
		<span class="reason"></span>
	</li>
 * 
 * 发起申请
 * <li class="first">
		<i class="colorIcon mui-badge">√</i>
		<span class="time">2019-02-23 11:00:44</span>
		<span class="nurseName">黄灿灿</span>
		<span class="status">发起申请</span>
		<span class="reason">家中有事，希望25号上夜班</span>
	</li>
 */